//
//  ShortVideoLogic.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/2/27.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "ShortVideoLogic.h"


@implementation ShortVideoLogic
- (instancetype)init
{
    self = [super init];
    if (self) {
        _page = 1;
        _dataArray = [NSMutableArray array];
    }
    return self;
}




-(void)loadData
{
    if (_page == 1) {
        [_dataArray removeAllObjects];
    }
    

    
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"small_video_api" A:@"index"];
    
    //如果id不为空的话获取某个人的视频
    if(StrValid(self.uid))
    {
        url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"short_video_api" A:@"index"];
    }
    
    if(_getMyVideo)
    {
        //更新接口v2
        url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"short_video_api" A:@"get_video_list"];
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        [param setObject:@(_page) forKey:@"page"];
        [CYNET POSTv2:url parameters:param responseCache:^(id responseObject) {
            
        } success:^(id responseObject) {
            if (_page == 1) {
                [_dataArray removeAllObjects];
            }
            NSArray *list = [NSArray modelArrayWithClass:[videoListModel class] json:[responseObject valueForKey:@"list"]];
            [_dataArray addObjectsFromArray:list];
            
            if (self.delegagte && [self.delegagte respondsToSelector:@selector(requestDataCompleted)]) {
                [self.delegagte requestDataCompleted];
            }
        } failure:^(NSString *error) {
            [[HUDHelper sharedInstance] tipMessage:error];
            //        [MBProgressHUD showTipMessageInWindow:error];
            if (self.delegagte && [self.delegagte respondsToSelector:@selector(requestDataCompleted)]) {
                [self.delegagte requestDataCompleted];
            }
        } hasCache:YES];
        
        return;
    }
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@(_page) forKey:@"page"];
    if(!StrValid(self.type))
    {
        self.type = @"";
    }
    [param setObject:self.type forKey:@"type"];

    [CYNET POST:url parameters:param responseCache:^(id responseObject) {

    } success:^(id responseObject) {
        if (_page == 1) {
            [_dataArray removeAllObjects];
        }
        NSArray *list = [NSArray modelArrayWithClass:[videoListModel class] json:responseObject];
        [_dataArray addObjectsFromArray:list];
        
        if (self.delegagte && [self.delegagte respondsToSelector:@selector(requestDataCompleted)]) {
            [self.delegagte requestDataCompleted];
        }
    } failure:^(NSString *error) {
        [[HUDHelper sharedInstance] tipMessage:error];
        //        [MBProgressHUD showTipMessageInWindow:error];
        if (self.delegagte && [self.delegagte respondsToSelector:@selector(requestDataCompleted)]) {
            [self.delegagte requestDataCompleted];
        }
    } hasCache:YES];
}

-(void)followVideo:(videoListModel *)model :(onSuccess)success
{
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"short_video_api" A:@"follow_video"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:StringFromInt(model.id) forKey:@"video_id"];
    
    [CYNET POSTv2:url parameters:param responseCache:^(id responseObject) {
        
    } success:^(id responseObject) {
        success();
    } failure:^(NSString *error) {
        [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];

}
@end
